jQuery.noConflict();
//var host=document.location.host;
//alert(host);
var num = 0;
/**
 * 加载数据
 */
function getSource(start,limit,fsiteid){
	start = start == undefined ? 0 : start;
	var data = {"start": start,"limit":limit};
	jQuery.ajax({
		url:"list?fsiteid="+fsiteid,
		data:data,
		type:"get",
		dataType:"json",
		cache:false,
		success:function(data){
			//bindGridList(data);
		//alert($('choose_node').value);
		if($('choose_node').value=="")
		{
		        josnTreeList(data);
		}
		else
		{
			josnTreeList(data,$('choose_node').value);
		}
		        //tree.terminate();
			//pager(data.total,data.limit,start);
			//if(!limit)
				//page(data.total,data.limit,1,"pager","getSource",true);
		},
		error:function(data){
			
			$.growlUI('数据加载出错'); 
		}
	   
	});
}
//添加栏目再加载
function regetSource(select_id){
	/*
	var start = 0; 
	var limit = 1000;
	var sort = $('sort').value;
	var fcolumnlevel = $('fcolumnlevel').value;
	var fcolumngp = $('fcolumngp').value;
	var fsiteid = $('fsiteid').value;
	var ID = $('ID').value;
	var fcolumnname = $('fcolumnname').value;
	var fcolumnurl = $('fcolumnurl').value;
	var IndexTemplate = $('IndexTemplate').value;
	var fchecked = $('fchecked').value;
	var choose_node = $('choose_node').value;
	*/
		imgLink.gennerJSON();
		
		if(setIndexPro){
			jQuery("#mar_source").val('{"marquee":[{"location":"index"}],"name":"marquee"},');
			setIndexPro = false;
		}
		
		document.form1.action = "addcolumn";
		document.form1.submit();
		
	/*
	var data = {"start": start,"limit":limit,"sort":sort,"fcolumnlevel":fcolumnlevel,"fcolumngp":fcolumngp,"fsiteid":fsiteid,"fcolumnname":fcolumnname,"IndexTemplate":IndexTemplate,"fchecked":fchecked,"choose_node":select_id};
	jQuery.ajax({
		url:"addcolumn",
		data:data,
		type:"post",
		dataType:"json",
		cache:false,
		success:function(data){
			//josnTreeList(data,select_id);
		//$('choose_node').value = select_id;
		//document.form1.action = "refreshcolumn";
		//document.form1.submit();
		},
		error:function(data){
			
			jQuery.growlUI('数据加载出错'); 
		}
	   
	});*/
}
var columnKeeper={
		showDialog:function()
		{
			var kdialog = KindEditor.dialog({
				width : 450,
				height: 400,
				title : '栏目导出数据列表',
				body : '<div id="txt_source_div" style="padding:10px;height:300px;overflow:auto;overflow-x:hidden;"></div>',
				shadowMode:true,
				closeBtn : {
					name : '关闭',
					click : function(e) {
						kdialog.remove();
					}
				},
				yesBtn : {
					name : '导入',
					click : function(e) {
						var sourceid = jQuery("input[type='checkbox'][name='txt_import']:checked").size();
						if(sourceid > 0){
							//_waiting._show('正在导人文章');
							
							var sourceName = "";
							jQuery("input[type='checkbox'][name='txt_import']:checked").each(function(){
								sourceName += ',' + jQuery(this).val();
							});
							//alert(sourceName);
							//alert($('fsiteid').value);
							jQuery.ajax({
								url:'columnImport',
								type:'post',
								dataType:'json',
								data:{'type':2,'txtNameStr':sourceName,'FSiteId':$('fsiteid').value},
								success:function(data){
									if(data > 0){
										jQuery.growlUI('数据导入成功');
										josnTreeList(data);
										//queryTcolSource(1);
										//_waiting._hide();
									}else{
										jQuery.growlUI('数据导入失败');
										//_waiting._hide();
									}
								},
								error:function(msg){
									jQuery.growlUI('数据导入出错');
									//_waiting._hide();
								}
							});
						}else{
							jQuery.growlUI('请选择数据文件');
						}
					}
				},
				noBtn : {
					name : '删除',
					click : function(e) {
						//aritcleGenneration.delete_txt();
					}
				}
			});
		},
		loadSource:function(data){
			var html = '';
			var dataLen = data.length;
			var txtName;
			jQuery.each(data,function(i,u){
				txtName = data[dataLen-i-1];
			  var	line = '<div id="import_txt_id'+i+'" style="height:25px;verticle-align:middle;line-height:25px;border:1px solid #ccc;padding-left:5px;margin:3px;	">'+txtName
			  			 +'<input id="'+i+'" type="checkbox" name="txt_import" onclick="columnKeeper.backgroundDiv(this)" value="'+txtName.substring(0,txtName.length-4)+'" style="float:right;margin-right:5px;margin-top:6px;"/></div>';
			  html += line;
			});
			jQuery("#txt_source_div").html(html);
		},
		backgroundDiv:function(obj){
			var div_id = obj.id;
			if(obj.checked){
				jQuery("#import_txt_id"+div_id).css("background","#ccc");
			}else{
				jQuery("#import_txt_id"+div_id).css("background","#ffffff");
			}
		}
		
		
		
		
		
		
}
//发布/取消发布栏目再加载
function publish(check,fsiteid,ID){
	var checkvalue = check;
	var ID_STRING="";
	for(var DList=0;DList<ID.length;DList++)
	{
		ID_STRING = ID_STRING + ID[DList] + ",";
	}
	//alert(ID_STRING);
	//alert(checkvalue);
	
	var start = 0; 
	var limit = 1000;
	var data = {"start": start,"limit":limit,"ID": ID_STRING,"fsiteid":fsiteid,"checkvalue":checkvalue};
	jQuery.ajax({
		url:"publishcolumn",
		data:data,
		type:"post",
		dataType:"json",
		cache:false,
		success:function(data){
			josnTreeList(data,ID[0],"");
			if(checkvalue==1)
			{
				jQuery.growlUI('发布成功');
			}
			else
			{
				jQuery.growlUI('已取消发布');
			}
		},
		error:function(data){
			
			jQuery.growlUI('数据加载出错'); 
		}
	   
	});
}
//清空栏目
function clear(ID){
	var data = {"ID": ID};
	jQuery.ajax({
		url:"clearcolumn",
		data:data,
		type:"post",
		dataType:"json",
		cache:false,
		success:function(data){
			if(data.res==0)
			{
				alert("没有找到与该栏目关联的文章");
			}
			else if(data.res>0)
			{
				alert("所有文章关联清空完成");
			}
		},
		error:function(data){
			
			jQuery.growlUI('数据加载出错'); 
		}
	   
	});
}
//删除栏目
function delete_column(Array_ID,fsiteid){
	//alert(Array_ID.length);
	var ID_STRING="";
	for(var DList=0;DList<Array_ID.length;DList++)
	{
		ID_STRING = ID_STRING + Array_ID[DList] + ",";
	}
	//ID_STRING=ID_STRING.toString().substring(0,ID_STRING.length-1);
	//alert(ID_STRING);
	//document.form1.action = "deletecolumn";
	//document.form1.submit();
	
	var data = {"ID": ID_STRING};
	jQuery.ajax({
		url:"deletecolumn?fsiteid="+fsiteid,
		data:data,
		type:"post",
		dataType:"json",
		cache:false,
		success:function(data){
		    //jQuery.growlUI('删除成功');
		//alert(data.source[0].fsiteid);
		    //jQuery.growlUI('删除成功');
		    document.form1.action = "refreshcolumn";
			document.form1.submit();
		},
		error:function(data){
			//document.form1.action = "refreshcolumn";
			//document.form1.submit();
			jQuery.growlUI('删除失败'); 
		}
	   
	});
	
}
//修改栏目顺序
function update_column(ArrayColumnList){
	//location.href = "updatecolumn?param="+ArrayColumnList;
	//alert(cList);
	//var data = {"param": ArrayColumnList};
	//alert(ArrayColumnList);
	jQuery.ajax({
		url:"updatecolumn?param="+ArrayColumnList,
		//data:data,
		type:"GET",
		dataType:"json",
		cache:false,
		success:function(data){
		    document.form1.action = "refreshcolumn";
			document.form1.submit();
		},
		error:function(data){
			
			document.form1.action = "refreshcolumn";
			document.form1.submit();
		}
	   
	});
	
}
//获取传入栏目的子栏目最高排序号
function getCount(start,limit,fsiteid,id,nowsort,level,node_fsortnum){
	if(fsiteid==""){ return;}
	var rate = "0.1";
	$('sort').value = (node_fsortnum*1+rate*1);
	//alert($('sort').value);
	regetSource(id);
}
function josnTreeList(data){
	//清空一次tree层
	var selected_id;var pub=false;
	selected_id = arguments.length>=2? arguments[1]:null;
	if(arguments.length==3){ pub = true;};
	$('tree_container').innerHTML = "";
	num++;
	if(num>1)
	{
		document.form1.action = "refreshcolumn";
		document.form1.submit();
	}
	//重新生成tree
	tree = new Mif.Tree({
		container: $('tree_container'),
		forest: false,
		initialize: function(){
		    //this.initCheckbox('simple');
			new Mif.Tree.KeyNav(this);
			
			new Mif.Tree.Drag(this, {
				onCheck: function(node){
					//$('log').adopt(new Element('li').set('html', node.name+' checked'));
				alert("FFF");
				},
			    onUnCheck: function(node){
				    //$('log').adopt(new Element('li').set('html', node.name+' unchecked'));
			     },
				onDrag: function(){
					//inject book inside book not allowed;
					if(this.target && this.target.type=='book' && this.current.type=='book' && this.where=='inside'){
						this.where='notAllowed';
					}//alert(this.where)
					//$('destination').innerHTML=this.target ? this.target.name : '';
					//$('where').innerHTML=this.where;
				},
				onStart: function(){
					
					//alert(this.where+"||"+this.target.name);
					//$('source').innerHTML=this.current.name;
					//alert(this.current.id+":"+this.current.name);
				},
				onComplete: function(){
					//alert(this.where+"||"+this.target.name);
					if(this.where=="after")
					{
						point = this.target;
						current = this.current;
						if(confirm('确认将栏目<'+current.name+'>作为<'+point.name+'>的同级栏目移动至其下方位置?'))
						{
							if(!point.isOpen()) point.toggle();
							if(!current.isOpen()) current.toggle();
							var seNode = point;
							var target_gp = point.fcolumngp;
							var target_fcolumnlevel = parseInt(seNode.fcolumnlevel);
							var addlevel = parseInt(point.fcolumnlevel);
							if(seNode.getNextVisible()!=false&&parseInt(seNode.getNextVisible().fcolumnlevel)==parseInt(seNode.fcolumnlevel)+1)
							{
								seNode = seNode.getNextVisible();
								while(parseInt(seNode.getNextVisible().fcolumnlevel)>=parseInt(point.getNextVisible().fcolumnlevel))
								{
									seNode = seNode.getNextVisible();
								}
							}
							var target_name = seNode.name;
							var target_fsortnum =  seNode.fsortnum;
							var target_fcolumngp =  seNode.fcolumngp;
							
							//if(!this.target.isOpen()) this.target.toggle();
							Node = this.current;
							if(target_fcolumnlevel==1)
							{
								target_gp = Node.id;
									
							}
							var kk=true;
							var sFnum = 0.00001;
							var sort = parseFloat(target_fsortnum)+sFnum;
							var nowlevel = target_fcolumnlevel;
							var offset = nowlevel-parseInt(Node.fcolumnlevel);
							//alert(nowlevel);
							var move_list = [Node.id+"|"+nowlevel+"|"+target_gp+"|"+sort];
							//alert(move_list);
							while(kk)
							{
								    
								    if(Node.getNextVisible()==false)
								    {kk=false;}
								    else
								    {
								    	Node = Node.getNextVisible();
								    	
								    	if(parseInt(this.current.fcolumnlevel)<parseInt(Node.fcolumnlevel))
								    	{
								    		sFnum = sFnum + 0.00001;
								    		sort = parseFloat(target_fsortnum)+sFnum;
								    		nowlevel = parseInt(Node.fcolumnlevel)+offset;
								    		//alert(nowlevel);
											move_list.push(Node.id+"|"+nowlevel+"|"+Node.fcolumngp+"|"+sort);
											//调试用
											
											//tree.select(node);
											//alert(Node.id);
										   	
								    	}
								    	else
								    	{
								    		kk = false;
								    	}
								    	
								    	
								    }
							}
							var cList="";
							for(var MList=0;MList<move_list.length;MList++)
							{
								cList = cList+move_list[MList]+",";
							}
							
							cList=cList.substring(0,cList.length-1);
							//alert(cList);
							update_column(cList);
						}else{
							this.where='notAllowed'; 
						}
					}
					if(this.where=="inside")
					{
						point = this.target;
						current = this.current;
						
						if(confirm('确认将栏目<'+current.name+'>作为<'+point.name+'>的子栏目?'))
						{
							
							if(!point.isOpen()) point.toggle();
							if(!current.isOpen()) current.toggle();
							
							var seNode = point;
							var target_id = point.id;
							var target_fcolumnlevel = parseInt(seNode.fcolumnlevel);
							var addlevel = parseInt(point.fcolumnlevel);
							if(seNode.getNextVisible()!=false&&parseInt(seNode.getNextVisible().fcolumnlevel)==parseInt(seNode.fcolumnlevel)+1)
							{
								seNode = seNode.getNextVisible();
								while(parseInt(seNode.getNextVisible().fcolumnlevel)>=parseInt(point.getNextVisible().fcolumnlevel))
								{
									seNode = seNode.getNextVisible();
								}
							}
							var target_name = seNode.name;
							var target_fsortnum =  seNode.fsortnum;
							var target_fcolumngp =  seNode.fcolumngp;
							
							var first_gp;
							
							Node = this.current;
							if(target_name==this.tree.root.name&&!target_fsortnum)
							{
								target_fcolumnlevel = 0;
								target_name = this.tree.root.getLast().name;
								target_fcolumngp = current.id;
								var lastNode=this.tree.root.getLast();
					    		while(lastNode.getNextVisible()!=false)
					    		{
					    		   lastNode = lastNode.getNextVisible();	
					    		}
					    		target_fsortnum = lastNode.fsortnum;
					    		first_gp = Node.id;
							}
							else
							{
								first_gp = target_id;
							}
							var kk=true;
							var sFnum = 0.00001;
							var sort = parseFloat(target_fsortnum)+sFnum;
							var nowlevel = target_fcolumnlevel+1;
							var offset = nowlevel-parseInt(Node.fcolumnlevel);
							//alert(nowlevel);
							var move_list = [Node.id+"|"+nowlevel+"|"+first_gp+"|"+sort];
							//alert(move_list);
							while(kk)
							{
								    
								    if(Node.getNextVisible()==false)
								    {kk=false;}
								    else
								    {
								    	Node = Node.getNextVisible();
								    	
								    	if(parseInt(this.current.fcolumnlevel)<parseInt(Node.fcolumnlevel))
								    	{
								    		sFnum = sFnum + 0.00001;
								    		sort = parseFloat(target_fsortnum)+sFnum;
								    		nowlevel = parseInt(Node.fcolumnlevel)+offset;
								    		//alert(nowlevel);
											move_list.push(Node.id+"|"+nowlevel+"|"+Node.fcolumngp+"|"+sort);
											//调试用
											
											//tree.select(node);
											//alert(Node.id);
										   	
								    	}
								    	else
								    	{
								    		kk = false;
								    	}
								    	
								    	
								    }
							}
							var cList="";
							for(var MList=0;MList<move_list.length;MList++)
							{
								cList = cList+move_list[MList]+",";
							}
							
							cList=cList.substring(0,cList.length-1);
							update_column(cList);
							
						}else{
							this.where='notAllowed'; 
						}
					}
					if(this.where=="before")
					{
						point = this.target;
						current = this.current;
						if(confirm('确认将栏目<'+current.name+'>作为<'+point.name+'>的同级栏目移动至其上方位置?'))
						{
							if(!point.isOpen()) point.toggle();
							if(!current.isOpen()) current.toggle();
							var seNode = point;
							var target_gp = point.fcolumngp;
							var target_fcolumnlevel = parseInt(seNode.fcolumnlevel);
							var addlevel = parseInt(point.fcolumnlevel);
							if(parseInt(point.fsortnum)>1)
					    	{
								seNode = seNode.getPreviousVisible();
					    	}
							var target_name = seNode.name;
							var target_fsortnum =  seNode.fsortnum;
							if(parseInt(point.fsortnum)==1)target_fsortnum =  0;
							var target_fcolumngp =  seNode.fcolumngp;
							
							//if(!this.target.isOpen()) this.target.toggle();
							Node = this.current;
							if(point.fcolumnlevel==1)
							{
								target_gp = Node.id;
									
							}
							var kk=true;
							var sFnum = 0.00001;
							var sort = parseFloat(target_fsortnum)+sFnum;
							var nowlevel = target_fcolumnlevel;
							var offset = nowlevel-parseInt(Node.fcolumnlevel);
							//alert(nowlevel);
							var move_list = [Node.id+"|"+nowlevel+"|"+target_gp+"|"+sort];
							//alert(move_list);
							while(kk)
							{
								    
								    if(Node.getNextVisible()==false)
								    {kk=false;}
								    else
								    {
								    	Node = Node.getNextVisible();
								    	
								    	if(parseInt(this.current.fcolumnlevel)<parseInt(Node.fcolumnlevel))
								    	{
								    		sFnum = sFnum + 0.00001;
								    		sort = parseFloat(target_fsortnum)+sFnum;
								    		nowlevel = parseInt(Node.fcolumnlevel)+offset;
								    		//alert(nowlevel);
											move_list.push(Node.id+"|"+nowlevel+"|"+Node.fcolumngp+"|"+sort);
											//调试用
											
											//tree.select(node);
											//alert(Node.id);
										   	
								    	}
								    	else
								    	{
								    		kk = false;
								    	}
								    	
								    	
								    }
							}
							var cList="";
							for(var MList=0;MList<move_list.length;MList++)
							{
								cList = cList+move_list[MList]+",";
							}
							
							cList=cList.substring(0,cList.length-1);
							//alert(cList);
							update_column(cList);
						}else{
							this.where='notAllowed'; 
						}
					}
					//alert(this.where+"||"+this.target.name);
					//alert(this.target.name);
					//$('destination').innerHTML='';
					//$('where').innerHTML='';
					//$('source').innerHTML='';
					/*
					for(var MList=0;MList<move_list.length;MList++)
					{
						alert(move_list[MList]);
					}
					*/
				}
			});
			
		},
		types: {
			folder:{
				openIcon: 'mif-tree-open-icon',
				closeIcon: 'mif-tree-close-icon'
			},
			loader:{
				openIcon: 'mif-tree-loader-open-icon',
				closeIcon: 'mif-tree-loader-close-icon',
				dropDenied: ['inside','after']
			},
			disabled:{
				openIcon: '',
				closeIcon: '',
				dragDisabled: false,
				cls: 'disabled'
			},
			book:{
				openIcon: 'mif-tree-book-icon',
				closeIcon: 'mif-tree-book-icon',
				loadable: true
			},
			/*
			bin:{
				openIcon: 'mif-tree-bin-open-icon',
				closeIcon: 'mif-tree-bin-close-icon'
			}
			*/
			
			bin:{
				openIcon: 'mif-tree-bin-open-icon',
				closeIcon: 'mif-tree-bin-close-icon'
			}
		},
		dfltType:'folder',
		height: 18,
		onCopy: function(from, to, where, copy){
			if(from.getParent()==copy.getParent()){
				copy.set({
					property: {
						name: 'copy '+from.name
					}
				});
			}
		},
		onRename: function(node, newName, oldName){
		    //成功重命名后
		}
	})
	var json ="";
	if(data.source.length==0)
	{
		 json='[{"property": {"name": "'+data.Wname.ftitle+'","hasCheckbox": false},"type": "book","children":[{"property": {"name": "no column exist"},"type":"disabled"},';
	}
	else
	{
		
		json='[{"property": {"name": "'+data.Wname.ftitle+'","hasCheckbox": false},"type": "book","children":[';
	}
	var currentlevel = 1;
	var Index = 0;
	jQuery.each(data.source,function(i,columnTree){
		Index++;//alert(columnTree.fcolumnlevel);
		//alert(columnTree.fcolumnname);
		if(columnTree.fcolumnlevel == currentlevel)
		{
			//alert(columnTree.fchecked);
		json +='{"property":{"name":"'+columnTree.fcolumnname+'","id":"'+columnTree.id+'","ftemplate":"'+columnTree.ftemplateguid+'","fcolumnlevel":"'+columnTree.fcolumnlevel+'","fcolumngp":"'+columnTree.fcolumngp+'","fsortnum":"'+columnTree.fsortnum+'","fchecked":"'+columnTree.fchecked+'","forderway":"'+columnTree.forderway+'"}';
		if(columnTree.fchecked==0)
		{
			json +=',"type": "bin"';
		}
		json +='},';
		}
		else if(columnTree.fcolumnlevel > currentlevel)
		{
			//alert(columnTree.fchecked);
			currentlevel++;
			json = json.substring(0,json.length-2);
			json +=',"children":[{"property": {"name": "'+columnTree.fcolumnname+'","id":"'+columnTree.id+'","ftemplate":"'+columnTree.ftemplateguid+'","fcolumnlevel":"'+columnTree.fcolumnlevel+'","fcolumngp":"'+columnTree.fcolumngp+'","fsortnum":"'+columnTree.fsortnum+'","fchecked":"'+columnTree.fchecked+'","forderway":"'+columnTree.forderway+'"}';
			if(columnTree.fchecked==0)
			{
				json +=',"type": "bin"';
			}
			json +='},';
		}
		else if(columnTree.fcolumnlevel < currentlevel)
		{
			while(columnTree.fcolumnlevel!=currentlevel)
			{
				currentlevel--;
				json = json.substring(0,json.length-1);
				json +=']},';
			}
			//alert(columnTree.fchecked);
			json +='{"property":{"name":"'+columnTree.fcolumnname+'","id":"'+columnTree.id+'","ftemplate":"'+columnTree.ftemplateguid+'","fcolumnlevel":"'+columnTree.fcolumnlevel+'","fcolumngp":"'+columnTree.fcolumngp+'","fsortnum":"'+columnTree.fsortnum+'","fchecked":"'+columnTree.fchecked+'","forderway":"'+columnTree.forderway+'"}';
			if(columnTree.fchecked==0)
			{
				json +=',"type": "bin"';
			}
			json +='},';
		}
		//alert(json);
	})
	json = json.substring(0,json.length-1);
	json +=']';
	for(i=currentlevel;i>=1;i--)
	{
		json +='}]';
	}
	//alert(json);
		//tree.initSortable();
	//var json =[{"property": {"name": "21","hasCheckbox": false},"type": "book","children":[]}];
	//alert(json);
	json = eval(json);
	tree.load({
		
		//url: '../../js/mootools/forest.json'
		json : json
		
	})
    .addEvent('select',function(node){
    	
    	var node = tree.selected;
    	if(node.name!=data.Wname.ftitle){$('id_show').innerHTML= node.id;$('ID').value=node.id;}else{$('id_show').innerHTML= "";$('ID').value="";}
    	if(node.name!=data.Wname.ftitle){$('fcolumnname').value= node.name;}else{$('fcolumnname').value= "";}
//    	if(node.name!=data.Wname.ftitle){$('fcolumnurl').value= node.fcolumnurl;}else{$('fcolumnurl').value= "";}
    	if(node.name!=data.Wname.ftitle){$('IndexTemplate').value= node.ftemplate;}else{$('IndexTemplate').value= "";}
    	if(node.name=="no column exist")
		{
		    tree.select(tree.root);$('fcolumnname').value= "";
		}
    	if(node.name!=data.Wname.ftitle)
    	{
    		$('fcolumnlevel').value= node.fcolumnlevel;
    		var ColName = "";
    		switch(node.fcolumnlevel)
    		{
    		  case "1": ColName = "一级";break;
    		  case "2": ColName = "二级";break;
    		  case "3": ColName = "三级";break;
    		  case "4": ColName = "四级";break;
    		  case "5": ColName = "五级";break;
    		  case "6": ColName = "六级";break;
    		  case "7": ColName = "七级";break;
    		  case "8": ColName = "八级";break;
    		  case "9": ColName = "九级";break;
    		  case "10":ColName = "十级";break;
    		  case "11":ColName = "十一级";break;
    		  case "12":ColName = "十二级";break;
    		  case "13":ColName = "十三级";break;
    		  case "14":ColName = "十四级";break;
    		  case "15":ColName = "十五级";break;
    		  case "16":ColName = "十六级";break;
    		  case "17":ColName = "十七级";break;
    		  case "18":ColName = "十八级";break;
    		  case "19":ColName = "十九级";break;
    		  case "20":ColName = "二十级";break;
    		  case "21":ColName = "二十一级";break;
    		  case "22":ColName = "二十二级";break;
    		  case "23":ColName = "二十三级";break;
    		  case "24":ColName = "二十四级";break;
    		  case "25":ColName = "二十五级";break;
    		  case "26":ColName = "二十六级";break;
    		  case "27":ColName = "二十七级";break;
    		  case "28":ColName = "二十八级";break;
    		  case "29":ColName = "二十九级";break;
    		  case "30":ColName = "三十级";break;
    		}
    		$('level_show').innerHTML = ColName;
    	}
    	else
    	{
    		$('fcolumnlevel').value= "";
    		
    		$('level_show').innerHTML = "";
    	}
    	if(node.name!=data.Wname.ftitle){$('fcolumngp').value= node.fcolumngp;}else{$('fcolumngp').value= "";}
    	if(node.name!=data.Wname.ftitle){$('sort').value= node.fsortnum;}else{$('sort').value= "";}
    	if(node.name!=data.Wname.ftitle){$('fchecked').value= node.fchecked;}else{$('fchecked').value= "";}
    	
    	if($('id_show').innerHTML=="undefined")$('id_show').innerHTML="";
    	if($('fcolumnname').value=="undefined")$('fcolumnname').value="";
//    	if($('fcolumnurl').value=="undefined")$('fcolumnurl').value="";
    	if($('IndexTemplate').value=="undefined")$('IndexTemplate').value="";
    	if($('fcolumngp').value=="undefined")$('fcolumngp').value="";
    	if($('fchecked').value=="0"){$('check_show').innerHTML="未发布";}
    	else if($('fchecked').value=="1"){$('check_show').innerHTML="已发布";}
    	else{$('check_show').innerHTML="";}
    	
    	
    	jQuery("#setIndexPro").attr('checked',false);
    	jQuery("#ico_prew_url").val('');
		jQuery("#img_prew_show1").hide();
		jQuery("#ico_prew1").hide();
		
		jQuery("#col_img").html('');
		jQuery("#marquee_text1").val('');
		jQuery("#marquee_text2").val('');
		jQuery("#mar_source").val('');
		jQuery("#json_source").val('');
		jQuery("#msg_source").val('');
		jQuery("#marquee_arr1").html('');
		jQuery("#marquee_arr2").html('');
	//	jQuery("#articleST").get(0).options[0].selected = true;
		
    	/**
    	 * 加载图标
    	 */
    	if(node.name != data.Wname.ftitle){
	    	jQuery.ajax({
	    		url:'/CMS/main/columnByIdForICO?cid=' + node.id,
	    		type:'post',
	    		dataType:'json',
	    		success:function(data){
	    			
	    			if(data.fcolumnurl != ''){
	    				
	    				
	    				
	    				
		    			var dataArr = eval('[' + data.fcolumnurl + ']');
		    			for(var i = 0;i < dataArr.length;i++){
		    				if(dataArr[i] != undefined && dataArr[i].name == "CATTR"){
		    					jQuery("#ico_prew_url").val(dataArr[i].data[0].ICO);
		    					jQuery("#img_prew_show1").show();
		    					jQuery("#ico_prew1").attr('src',dataArr[i].data[0].ICO);
		    					jQuery("#ico_prew1").show();
		    				}else if(dataArr[i] != undefined && dataArr[i].name == "marquee"){
		    					
			    				if( dataArr[i].marquee[0].location == "index"){
			    					jQuery("#setIndexPro").attr('checked',true);
			    					jQuery("#mar_source").val(data.fcolumnurl);
			    				}
		    					
//		    					var text = "",marArr = dataArr[i].marquee;
//		    					for(var m = 0;m < marArr.length; m++){
//		    						text += '"'+ marArr[m] +'"';
//		    						jQuery("#marquee_arr1").append('<span class="marquee_t" id="marquee_t_1'+ m +'"><img src="/CMS/images/tree/icon_tip.gif" /><span class="marquee_text">' + marArr[m] + '</span><img src="/CMS/images/tree/icon_cancel.gif" title="删除" onclick="cancerMar(1,'+ m +')" style="float:right;margin-right:10px;cursor:pointer;"/></span>');
//		    					}
//		    					
//		    					jQuery("#mar_source1").val('{"marquee":[' + text.substring(0,text.length - 1) + '],"name":"marquee"},');
		    				}else if(dataArr[i] != undefined && dataArr[i].name == "SMS"){
		    					var text = "",messArr = dataArr[i].SMS;
		    					for(var m = 0;m < messArr.length; m++){
		    						text += '{"text":"'+ messArr[m] +'"},';
		    						jQuery("#marquee_arr2").append('<span class="marquee_t" id="marquee_t_2'+ m +'"><img src="/CMS/images/tree/icon_tip.gif" /><span class="marquee_text">' + messArr[m].text + '</span><img src="/CMS/images/tree/icon_cancel.gif" title="删除" onclick="cancerMar(2,'+ m +')" style="float:right;margin-right:10px;cursor:pointer;"/></span>');
		    					}
		    					
		    					jQuery("#mar_source2").val('{"marquee":[' + text.substring(0,text.length - 1) + '],"name":"SMS"},');
		    				}else if(dataArr[i] != undefined && dataArr[i].name == "img"){
		    					var imgArr = dataArr[i].img,imgs = '{"name":"img","img":[';
		    					
		    					for(var k = 0;k < imgArr.length; k ++){
		    						jQuery("#col_img").append('<div id="l_i_' + tag + '" class="text_box" onmouseover="imgLink.showLayer(1,' + tag + ');" onmouseout="imgLink.showLayer(2,' + tag + ');">\
		    								<img id="img_src_Source_'+tag+'" src="' + imgArr[k].src + '" width="180px" height="120px"/>\
		    								<div class="text_t"></div>\
		    								<div id="div_url_Source_'+tag+'" class="text_t_show">' + imgArr[k].url + '</div>\
		    								<div id="text_bg' + tag + '" class="text_bg" onmouseout="imgLink.showLayer(2,' + tag + ');"></div>\
		    								<div id="text_bg_t' + tag + '" class="text_bg_t">\
		    									<a href="javascript:void(0);" style="color:#ffffff;" title="删除图片" onclick="imgLink.remove('+tag+');">删除</a>\
		    									<a href="javascript:void(0);" style="color:#ffffff;" title="往前移动" onclick="moveDivPannel.before(' +tag+ ');">向前</a>\
		    									<a href="javascript:void(0);" style="color:#ffffff;" title="往后移动" onclick="moveDivPannel.after(' +tag+ ');">向后</a>\
		    								</div>\
		    								</div>');
		    						imgs += '{"src":"' + imgArr[k].src + '","url":"' + 	imgArr[k].url + '"},';
		    						tag ++;
		    					}
		    					imgs += ']},';
		    					jQuery("#json_source").val(imgs);
		    					
		    				}else if(dataArr[i] != undefined && dataArr[i].name == "linkType"){
		    					var sTypeO = dataArr[i].linkType;
		    					var t = 0;
		    					jQuery("#articleST option").each(function(){
		    						if(jQuery(this).val() == sTypeO[0].sType){
		    							jQuery("#articleST").get(0).options[t].selected = true;
		    						}
		    						t++;
		    					});
		    				}
		    			}
		    		
		    			
	    			}
	    		},
	    		error:function(){
	    		}
	    	});
    	}
	});
	//alert(num);
	var root = data.Wname.ftitle;
		tree.root.recursive(function(){
			
			this.toggle();
		})
		 //tree.select(tree.root);
		//alert(selected_id);
			//var node = tree.root.getFirst().getNextVisible().getNextVisible().getNextVisible().getNextVisible().id;
		//alert(selected_id);
		//selected_id = selected_id.replace("S","");
		//alert((""+selected_id).indexOf("S"));
		//if((""+selected_id).indexOf("S")!=-1){selected_id = parseInt((""+selected_id).replace("S",""));} 
		if(tree.root.getFirst().name=="no column exist")
		{
		    tree.select(tree.root);
		    
		}
		else
		{
			
			if(selected_id==null||selected_id=="undefined")
			{ 
					tree.select(tree.root);
			}
			else if(selected_id=="last")
			{ 
					tree.select(tree.root.getLast());
			}
			else
			{
				
				var jj=true;
				var node = tree.root;
				if((""+selected_id).indexOf("S")!=-1)
				{
					selected_id = parseInt((""+selected_id).replace("S",""));
					while(jj)
					{
						if(node.getNextVisible()!=false)
						{
							node = node.getNextVisible();
							if(node.id==selected_id)
							{
								
							    	tree.select(node);
							    	jj=false;
							    
							}
						}
						
					}
				}
				else
				{
					while(jj)
					{
						if(node.getNextVisible()!=false)
						{
							node = node.getNextVisible();
							if(node.id==selected_id)
							{
								//alert(node.id);
							    if(pub==false)
							    {
							    	if(node.getLast()==null){tree.select(node);}
							    	else{tree.select(node.getLast());}
							    	jj=false;
							    }
							    else
							    {
							    	tree.select(node);
							    	jj=false;
							    }
							}
						}
						
					}
				}
			}
		}
	var	first_id = tree.root.getFirst().id;
	var last_id = tree.root.getLast().id;
		//alert(node);
		
	/*
	$('rename').addEvent('click', function(){
		var node=tree.selected;
	    if(!node) return;
	    if($('fcolumnname').value=="")
	    {alert("栏目名称不可以为空"); $('fcolumnname').focus();return;}
	    node.rename();
	});
	*/
	$('tree_container').focus();
	$('tree_container').onkeypress=trace;
	function trace(){

		var val= event.keyCode;
        //alert(val);

		switch(val){
			case 119:
				$('MovePrevious').click();
			break;
			case 115:
				$('MoveNext').click();
				break;
			case 116:
				if(!tree.selected.isOpen())
				{
					tree.selected.toggle();
				}
				else
				{
					//tree.selected.goLeft();
					//tree.selected.toggle();
				}
				break;
			case 13:
				if($('fsiteid').value==""||$('fsiteid').value=="--请选择站点--")
				{
					
				}
				else
				{
					$('add_node').click();
				}
			break;
			case 100:
				$('BtnDel').click();
			break;
			case 114:
				//window.location.reload();
				document.form1.action = "refreshcolumn";
				document.form1.submit();
			break;


		}

	}
	$('BtnSave').addEvent('click', function(){
		if(tree.selected==tree.root)
		{
			return false;
		}
		
				$('choose_node').value = "S"+tree.selected.id;
		
		imgLink.gennerJSON();		
				
		document.form1.action = "savecolumn";
		document.form1.submit();
	});
	$('BtnDel').addEvent('click', function(){
		
		if(tree.selected==tree.root){{jQuery.growlUI("请选择要删除的栏目");return;}}
		var node=tree.selected;
		if(!node.isOpen()) node.toggle();
		if($('fsiteid').value==""||$('fsiteid').value=="--请选择站点--"){return;}
		/*
		if(confirm('确定要将栏目<'+node.name+'>及其关联的子栏目删除吗？')){
			document.form1.action = "deletecolumn";
			document.form1.submit();
		}else{*/
		if(confirm('确定要将栏目<'+node.name+'>及其关联的子栏目删除吗？')){
			var index_d = 0;
			var select_fcolumnlevel = node.fcolumnlevel;
			var select_id = node.id;
			var id_list = [select_id];
			var ii=true;
	        
			var start_fcolumnlevel = node.fcolumnlevel;
			var start_node = node;
			var kk=true;
			
			while(kk)
			{
				
				    if(node.getNextVisible()==false)
				    {kk=false;}
				    else
				    {
				    	node = node.getNextVisible();
				    	
				    	if(parseInt(start_fcolumnlevel)<parseInt(node.fcolumnlevel))
				    	{
							id_list.push(node.id);
							//调试用
							/*
							tree.select(node);
							alert(node.id);
							*/
				    	}
				    	else
				    	{
				    		kk = false;
				    	}
				    	
				    	
				    }
			}
			//alert(start_node.id);
			if(start_node.getPreviousVisible())
		    {
					$('choose_node').value = start_node.getPreviousVisible().id;
			}
			else
			{
					$('choose_node').value = "";
			}
			//tree.select(start_node.getPreviousVisible());
			//alert($('choose_node').value);
			delete_column(id_list,$('fsiteid').value);
			
			
		}else{}
	}
	/*}*/);
	tree.options.beforeRename=function(node, oldName, newName){
		if(confirm('确认将栏目<'+oldName+'>重新命名为<'+newName+'>?')){
			this.renameComplete();
		}else{
			this.renameCancel();
		}
	};
	
	$('BtnPublish').addEvent('click', function(){
		
		if($('fcolumnname').value==""){{jQuery.growlUI("请选择要发布的栏目");return;}}
		var current=tree.selected;
		$('choose_node').value = "S"+current.id;
		
		var id_list = [current.id];
		var ii=true;
        node = current;
		var start_fcolumnlevel = node.fcolumnlevel;
		var start_node = node;
		var kk=true;
		
		while(kk)
		{
			
			    if(node.getNextVisible()==false)
			    {kk=false;}
			    else
			    {
			    	node = node.getNextVisible();
			    	
			    	if(parseInt(start_fcolumnlevel)<parseInt(node.fcolumnlevel))
			    	{
						id_list.push(node.id);
						//调试用
						/*
						tree.select(node);
						alert(node.id);
						*/
			    	}
			    	else
			    	{
			    		kk = false;
			    	}
			    	
			    	
			    }
		}
		/*
		for(i=0;i<id_list.length;i++)
		{
			alert(id_list[i]);
		}*/
		//publish("1",$('fsiteid').value,current.id);
		publish("1",$('fsiteid').value,id_list);
		
		
	});
	
	$('BtnCancel').addEvent('click', function(){
		
		if($('fcolumnname').value==""){{jQuery.growlUI("请选择要取消发布的栏目"); return;}}
		var current=tree.selected;
		$('choose_node').value = "S"+current.id;
		var id_list = [current.id];
		var ii=true;
        node = current;
		var start_fcolumnlevel = node.fcolumnlevel;
		var start_node = node;
		var kk=true;
		
		while(kk)
		{
			
			    if(node.getNextVisible()==false)
			    {kk=false;}
			    else
			    {
			    	node = node.getNextVisible();
			    	
			    	if(parseInt(start_fcolumnlevel)<parseInt(node.fcolumnlevel))
			    	{
						id_list.push(node.id);
						//调试用
						/*
						tree.select(node);
						alert(node.id);
						*/
			    	}
			    	else
			    	{
			    		kk = false;
			    	}
			    	
			    	
			    }
		}
		
		publish("0",$('fsiteid').value,id_list);
		
		
	});
    $('BtnExport').addEvent('click', function(){
    	if(confirm('确定要备份网站<'+data.Wname.ftitle+'>下所有栏目？'))
    	{
    		jQuery.ajax({
				url:'columnExport',
				type:'post',
				dataType:'json',
				data:{'ID':$('fsiteid').value},
				success:function(data){
					jQuery.growlUI('导出成功！');
					//_waiting._hide();
					//aritcleGenneration._showDialog();
					//aritcleGenneration.loadSource(data);
				},
				error:function(msg){
					jQuery.growlUI('导出失败！');
					//_waiting._hide();
				}
			});
    	}
		
	});
    $('BtnImport').addEvent('click', function(){
    	//columnKeeper.showDialog();
    	
    	jQuery.ajax({
			url:'columnImport',
			type:'post',
			dataType:'json',
			data:{'type':1,'txtNameStr':"",'FSiteId':$('fsiteid').value},
			success:function(data){
				columnKeeper.showDialog();
				columnKeeper.loadSource(data);
				//aritcleGenneration._showDialog();
				columnKeeper.loadSource(data);
			},
			error:function(){
				jQuery.growlUI('数据加载出错');
			}
		});
		
	});	
	/*
    $('BtnClean').addEvent('click', function(){
		
		if($('fcolumnname').value==""){{jQuery.growlUI("请选择要清空文章关联的栏目"); return;}}
		var current=tree.selected;
		clear(current.id);
		
		
	});	
	*/
	/*
    $('MoveNext').addEvent('click', function(){
    	var current = tree.selected;
    	if(current.getNextVisible()!=false)
    	{
    		tree.select(current.getNextVisible());
    	}
    	else tree.select(tree.root);
    	    
	});
    $('MovePrevious').addEvent('click', function(){
    	var current = tree.selected;
    	if(current.getPreviousVisible()!=false)
    	{
    		tree.select(current.getPreviousVisible());
    	}
    	else
    	{
    		var lastNode=tree.root.getLast();
    		while(lastNode.getNextVisible()!=false)
    		{
    		   lastNode = lastNode.getNextVisible();	
    		}
    		tree.select(lastNode);
    	}
    	    
	});	
	*/
	$('add_node').addEvent('click', function(){
		//alert($('where').options[$('where').selectedIndex].text);
		var current=tree.selected;//alert(current.fsortnum);
		/*
		if(current.fcolumnlevel==7)
		{
			jQuery.growlUI("达到栏目层级上限");return;
		}
		*/
		if(!current){ jQuery.growlUI("请选择一条栏目"); return;}
		if($('fcolumnname').value==""&&!current){jQuery.growlUI("栏目名不能为空");return;}
		if(!current.isOpen()) current.toggle();
		if($('fsiteid').value==""||$('fsiteid').value=="--请选择站点--"){return;}
		node_ = current;
		while(node_.getNextVisible()!=false)
		{
			if(parseInt(node_.getNextVisible().fcolumnlevel)<=parseInt(current.fcolumnlevel))
			{
				break;
			}
			else
			{
				node_ = node_.getNextVisible();
				//alert(node_.id);
			}	
		}
		//alert(node_.id);
		if($('ID').value==""&&current){
			Index++;
			var Newsort;
			if($('fcolumnname').value=="")
			{
				if(tree.root.getLast().name == "no column exist")
				{
					Newsort = "0.1";
					tree.add({
						property: {name: '新建栏目',fcolumnlevel:1,fsortnum:Newsort,fcolumngp:9999}
				
					}, current, 'inside');
				}
				else
				{
					NewR = tree.root.getLast();
					while(NewR.getNextVisible()!=false)
					{
						NewR = NewR.getNextVisible();
					}
					Newsort = parseInt(NewR.fsortnum)+1;
					tree.add({
						property: {name: '新建栏目',fcolumnlevel:1,fsortnum:Newsort,fcolumngp:9999}
				
					}, current, 'inside');
				}
			}else {
				
				if(tree.root.getLast().name == "no column exist")
				{
					Newsort = "0.1";
				tree.add({
					property: {name: $('fcolumnname').value,fcolumnlevel:1,fsortnum:Newsort,fcolumngp:9999}
			//chrome//}, current, $('where').getElement(':selected').innerHTML);
				}, current, 'inside');
				}
				else
				{
					NewR = tree.root.getLast();
					while(NewR.getNextVisible()!=false)
					{
						NewR = NewR.getNextVisible();
					}
					Newsort = parseInt(NewR.fsortnum)+1;
					tree.add({
						property: {name: $('fcolumnname').value,fcolumnlevel:1,fsortnum:Newsort,fcolumngp:9999}
					}, current, 'inside');
				}
				
			}
			$('fcolumnlevel').value=1;
			$('fcolumngp').value=9999;
			$('sort').value=Newsort;
			/*
			
			alert(sort);
			*///this.tree.$getIndex()
			$('choose_node').value = "last";
			//alert($('sort').value);
				regetSource(current.id);
			
			
			//delete tree;
		}
		else
		{
			Index++;
			/*
		tree.add({
			property: {name: $('fcolumnname').value}
	//chrome//}, current, $('where').getElement(':selected').innerHTML);
		}, current, 'inside');*/
		$('fcolumnlevel').value=parseInt(current.fcolumnlevel)+1;
		$('fcolumngp').value=current.id;	
		//alert(this.indexOf(current));
		$('choose_node').value = current.id;
		node_ = current;
		while(node_.getNextVisible()!=false)
		{
			if(parseInt(node_.getNextVisible().fcolumnlevel)<=parseInt(current.fcolumnlevel))
			{
				break;
			}
			else
			{
				node_ = node_.getNextVisible();
				//alert(node_.id);
			}	
		}
		//alert(node_.id);
		getCount(0,1000,$('fsiteid').value,current.id,current.fsortnum,current.fcolumnlevel,node_.fsortnum);
		}
		//delete tree;
		return false;
	});
}
